import {createRouter,createWebHashHistory} from 'vue-router';
import children from './children'
import store from '../store';
let routes = [
    {
        path:'/',
        redirect:"/home"
    },
    {
        name:"login",
        path:"/login",
        component:()=>import("../views/login/index.vue")
    },{
        name:"layout",
        path:"/layout",
        component:()=>import('../views/layout/index.vue'),
        children:children
    }
]

let router = createRouter({
    history:createWebHashHistory(),
    routes
})
router.beforeEach((to,from,next)=>{
    console.log(from,to)

    setNav(to.name)
    if(to.path !='/login'){
        // 判断是否登录
        if(store.state.token){
            next()
        }else{
            next("/login")
        }
    }else{
        next()
    }
    
})
//所用导航信息
let navObj = {
  users:["用户管理","用户列表"],
  roles:["权限管理","角色列表"],
  rights:["权限管理","权限列表"],
  goods:["商品管理","商品列表"],
  params:["商品管理","分类列表"],
  categories:["商品管理","商品分类"],
  orders:["订单管理","订单列表"],
  reports:["数据统计","数据报表"],
  home:[]
}


//定义一个setNav方法
let setNav = (name)=>{
  let list = navObj[name];
  store.commit("setNav",list)
}



export default router;